import { isNumberType } from '@/utils/formMakeUtils.js';
import { getConfigData } from '@/components/Common/relatedProcess/data';

/**
 * 设置关联表单的 statistics 字段
 * @param {*} linkFormElement 关联表单节点
 */
export const setLinkStatistics = (linkFormElement) => {
  if (linkFormElement.type !== 'linkForm') { return; }
  if (linkFormElement.options.isStatistics) {
    linkFormElement.options.statistics = [];
    if (linkFormElement.options && linkFormElement.options.remoteParams.isCustom) {
      // 自定义表单
      linkFormElement.subform.forEach(i => {
        if (isNumberType(i) || i.type === 'statistics') {
          linkFormElement.options.statistics.push(i.key);
        }
      });
    } else if (linkFormElement.options && linkFormElement.options.remoteParams.type) {
      // 固定表单
      const type = linkFormElement.options.remoteParams.type;
      const configData = getConfigData(type);
      if (configData && configData.numberField) {
        configData.numberField.forEach(i => {
          const hasKey = linkFormElement.subform.some(sub => sub.key === i.key);
          if (hasKey) linkFormElement.options.statistics.push(i.key);
        });
      }
    }
  } else {
    linkFormElement.options.statistics = [];
  }
};
